/*function that get the flash component*/   


zoyoe.uploader = new function(){
  this.crunch = false;
  this.flip_vertical = false;
  this.flip_horizontal = false;
  this.rotation = 0;
  this.wp = 1;
  this.hp = 1;
  this.folder = "";
  this.name = "";
  this.rename = "";
  this.action = "default";
};

zoyoe.uploader.Crunch = function(sel_id,crunch_id){
  this.crunch = true;
  this.selection_id = sel_id;
  this.crunch_id = crunch_id;
  var container = document.getElementById(crunch_id);
  var hp = this.hp;
  var wp = this.wp;
  var img = container.getElementsByTagName('img')[0];
  var sel = document.getElementById(sel_id);
  var area = sel.getElementsByTagName('div')[4];
  var width = img.offsetWidth;
  var height = img.offsetHeight;
  var ws = width*wp;
  var hs = height*hp;
  var center_x = container.offsetWidth/2;
  var center_y = container.offsetHeight/2;
  var min = 200;
  if(hs < min){min = hs;}
  if(ws < min){min = ws;}
  sel.style.top = container.offsetTop + img.offsetTop + 'px';
  sel.style.width = img.offsetWidth + 'px';
  sel.style.left = container.offsetLeft + img.offsetLeft + 'px';
  sel.style.height = img.offsetHeight + 'px';
  var top = center_y - min/2/hp - 2 - img.offsetTop;
  var left = center_x - min/2/wp - 2 - img.offsetLeft;
  area.style.top = top + "px"; 
  area.style.left = left + "px";
  area.style.width = (min/wp - 2) + "px";
  area.style.height = (min/hp - 2) + "px";
  sel.style.display = 'block';
  (function pack(){
    var corners = sel.getElementsByTagName('div');
    var wb = left + min/wp;
    var hb = top + min/hp;
    corners[0].style.top = (top - 5) + "px";
    corners[0].style.left = (left - 5) + "px";
    corners[1].style.top = (top - 5) + "px";
    corners[1].style.left = (wb - 5) + 'px';
    corners[2].style.top = (hb - 5) + 'px';
    corners[2].style.left = (wb - 5) + 'px';
    corners[3].style.top = (hb - 5) + 'px';
    corners[3].style.left = (left - 5) + "px";
/*
    corners[4].style.top = (top ) + 'px';
    corners[4].style.left = (left ) + "px";
    corners[4].style.opacity = 0.3;
    corners[4].style.border = dashed;
*/
  })();
  this.ShowThumbnail('thumb-img');
}
zoyoe.uploader.Rename = function(name){
  this.rename = name;
}
zoyoe.uploader.Fullpath = function(){
  if(this.folder && this.folder!=""){
    if(this.action == 'add'){
      return this.folder + "/" + this.rename;
    }else{
      return this.folder + "/" + this.name;
    }
  }else{
    return this.name;
  }
}
zoyoe.uploader.Delete = function(gname, name) {
  var url = "/gallery/imagelink/" + zoyoe.service_name + "/?path=" + gname + "/" + name + "&rename="+name;
  url += "&action=delete";
  window.location.href = url;
}
zoyoe.uploader.Confirm = function(action){
  this.action = action;
  var url  = "/gallery/imagelink/" + zoyoe.service_name + "/?path=" + this.Fullpath() + "&rename="+this.rename;
  
  url += "&action=" + this.action;
  if(this.flip_vertical){
    url += "&vflip=true";
  }
  if(this.flip_horizontal){
    url += "&hflip=true";
  }
  if(this.rotation!=0){
    url += "&rotation="+this.rotation;
  }
  url +="&top="+(this.crunch_top*10000);
  url +="&left="+(this.crunch_left*10000);
  url +="&wp="+Math.ceil(this.crunch_wp*10000);
  url +="&hp="+Math.ceil(this.crunch_hp*10000);
  window.parent.location.href = url;
}
zoyoe.uploader.ShowThumbnail = function(thumb_id){
  var container = document.getElementById(this.crunch_id);
  var img = container.getElementsByTagName('img')[0];
  var thumb_container = document.getElementById(thumb_id);
  var thumb_img = thumb_container.getElementsByTagName('img')[0];
  thumb_img.src = img.src;
  var sel = document.getElementById(this.selection_id);
  var area = sel.getElementsByTagName('div')[4];
  var p = thumb_container.offsetWidth/area.offsetWidth;
  thumb_img.style.width = (img.offsetWidth * p) + "px";
  thumb_img.style.height = (img.offsetHeight * p) + "px";
  var top = area.offsetTop * p * (-1); 
  var left = area.offsetLeft * p * (-1);
  thumb_img.style.top = top + "px";
  thumb_img.style.left = left + "px";
  zoyoe.uploader.crunch_top = area.offsetTop/img.offsetHeight;
  zoyoe.uploader.crunch_left = area.offsetLeft/img.offsetWidth;
  zoyoe.uploader.crunch_wp = Math.abs(area.offsetWidth/img.offsetWidth);
  zoyoe.uploader.crunch_hp = Math.abs(area.offsetHeight/img.offsetHeight);
}
zoyoe.uploader.genImageClass = function(){
  var className = "";
  if(this.flip_vertical){
    if(this.flip_horizontal){
      className += " flip-both";
    }else{
      className += " flip-vertical";
    }
  }else{
    if(this.flip_horizontal){
      className += " flip-horizontal";
    }
  }
  return className;
}
zoyoe.uploader.genImageSrc = function(){
  var url = "/gallery/imagecache/"+this.sname+"/data/?rotation="+this.rotation
  if(this.flip_vertical){
    url += "&vflip=true";
  }
  if(this.flip_horizontal){
    url += "&hflip=true";
  }
  if(this.rotation!=0){
    url += "&rotation="+this.rotation;
  }
  return url;
}
zoyoe.uploader.RotateCCW = function(dom_ele){
  if(zoyoe.uploader.crunch){
    var img = dom_ele.getElementsByTagName('img')[0];
    this.rotation = (this.rotation + 90 + 360) % 360;
    img.src = this.genImageSrc();
  }
}
zoyoe.uploader.RotateCW = function(dom_ele){
  if(zoyoe.uploader.crunch){
    var img = dom_ele.getElementsByTagName('img')[0];
    this.rotation = (this.rotation - 90 + 360) % 360;
    img.src = this.genImageSrc();
  }
}
zoyoe.uploader.FlipVertical = function(dom_ele){
  if(zoyoe.uploader.crunch){
    var img = dom_ele.getElementsByTagName('img')[0];
    this.flip_vertical = !this.flip_vertical;
    img.src = this.genImageSrc();
  }
}
zoyoe.uploader.FlipHorizontal = function(dom_ele){
  if(zoyoe.uploader.crunch){
    var img = dom_ele.getElementsByTagName('img')[0];
    this.flip_horizontal = !this.flip_horizontal;
    img.src = this.genImageSrc();
  }
}

function OpenHandler(){
   alert('File Open');
}
function SelectHandler(name){
   var uploader = zoyoe.uploader.entity;
   var s2 = document.getElementById('s2');
   s2.className = 'ready';
   s2.getElementsByTagName('span')[0].innerHTML = name;
   setTimeout("zoyoe.uploader.entity.UploadFile()",100);/* Probably a bug here, need allow flash to complete first */
}

function UpdateImgUrl(url){
  document.getElementById('resurl').innerHTML = url;
  zoyoe.uploader.Rename(url.split("/").pop());
}

zoyoe.uploader.Setup = function(sname){
  zoyoe.uploader.entity = (function(movieName){
    if (navigator.appName.indexOf("Microsoft") != -1) {
      return document.getElementById('ie_'+movieName);
      //return window[movieName];
    }else{
      if(document[movieName]){
        return document[movieName];
      }else{
        return document.getElementById(movieName);
      }
    }
  })('uploader');
  zoyoe.uploader.sname = sname;
  var uploader = zoyoe.uploader.entity;
  if(uploader){
    uploader.AddEventHandler('Select','SelectHandler');
    uploader.AddEventHandler('Progress','ProgressHandler');
    uploader.AddEventHandler('Complete','CompleteHandler');
    uploader.AddEventHandler('IOError','ErrorHandler');
    uploader.SetUrl("http://"+window.location.host+"/gallery/imagecache/"+sname+"/cache/");
  }else{
  }
  //alert (uploader.Status());
}


